package com.huang.controller;


import com.huang.mapper.DepartmentMapper;
import com.huang.mapper.EmployeeMapper;
import com.huang.pojo.Department;
import com.huang.pojo.Employee;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.sql.DataSource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class EmployeeController {

    @Autowired
    EmployeeMapper employeeMapper;


    //联表查全部
    @GetMapping("/queryAllList")
    public String queryAllList(Model model){
        List<Map<String,Object>> all = employeeMapper.queryAllList();
        model.addAttribute("alls",all);
        return "emp/allList";
    }

    //查员工
    @GetMapping("/queryEmployeeList")
    public String queryEmployeeList(Model model){
        List<Employee> employeeList = employeeMapper.queryEmployeeList();
        model.addAttribute("emps",employeeList);
        return "emp/list";
    }


    //增
    //问题：这里使用RequestMapping才可以,GetMapping，404,PostMapping，405
    @RequestMapping("/toadd")
    public String toAddPage(Model model){
        List<Employee> allDepName = employeeMapper.queryEmployeeList();
        model.addAttribute("depNames",allDepName);
        return "emp/add";
    }


    @RequestMapping("/addEmployee")
    public String addEmployee( Employee employee){
        System.out.println("save====>" + employee);
        employeeMapper.addEmployee(employee);

        return "redirect:/queryEmployeeList";
    }

    //跳转到修改，并提出原数据
    @RequestMapping("/updateEmployee/{id}")
    public String toUpdateEmployee(@PathVariable("id") Integer id,  Model model){
        Employee employee = employeeMapper.queryEmployeeById(id);//根据id查出原数据
        System.out.println("查出的元数据："+ employee);
        model.addAttribute("emp",employee);//返回给前台
        return "emp/updata";
    }

    //保存修改后的数据
    @RequestMapping("/updateEmployee")
    public String saveUpdateEmployee(Employee employee){
        employeeMapper.updateEmployee(employee);
        return "redirect:/queryEmployeeList";
    }

    //删除数据
    @RequestMapping("/deleteEmployee/{id}")//从前端获取要删除的数据的id的前端url地址
    public String deleteEmployee(@PathVariable("id") Integer id){//获取id
        employeeMapper.deleteEmployee(id);//根据id进行删除
        return "redirect:/queryEmployeeList";
    }
    /*
   @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employeeDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAddpage(Model model){
       //查出所有部门的信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        System.out.println("save=>"+employee);
       //调用底层业务方法保存员工信息
        employeeDao.save(employee);
        //重定向回list页面
        return "redirect:/emps";
    }


    //去员工的修改页面
    @GetMapping("/emp/{id}")
    public String toupDataEmp(@PathVariable("id")Integer id,Model model){
    //查出原来的数据
        Employee employee = employeeDao.getEmployeeById(id);
        //将查出的信息返回到前端
        model.addAttribute("emp",employee);

        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
       return "emp/updata";
    }

    @PostMapping("/updataEmp")
    public String updateEmp(Employee employee){
       employeeDao.save(employee);
       return "redirect:/emps";
    }

    //删除员工信息
    @GetMapping("/delemp/{id}")
    public String deleteEmp(@PathVariable("id") Integer id){
       employeeDao.deleteEmployeeById(id);
       return "redirect:/emps";
    }

     */
}
